package com.puppy.utils;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyuncs.exceptions.ClientException;

import java.io.ByteArrayInputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

//@Component
public class AliyunOSSOperator {
//    /// /       方法一：通过@Values()注解，一个属性一个属性的注入
//    @Value("${aliyun.oss.endpoint}")
//    private String endpoint ; //一.创建bucket的外网域名，自己去官网看
//    @Value("${aliyun.oss.bucketName}")
//    private String bucketName ;//二.创建bucket的名称，自己定义
//    @Value("${aliyun.oss.region}")
//    private String region ;//三：替换为您的 Bucket 区域

//    String endpoint = "https://oss-cn-beijing.aliyuncs.com"; //一.创建bucket的外网域名-oss的访问路径，自己去官网看
//    String bucketName = "keats-java-ai";//二.创建bucket的名称，自己定义
//    String region = "cn-beijing";// 三：替换为您的 Bucket 区域

    //    @Autowired
    private final AliyunOSSProperties aliyunOSSProperties;//配置变量

    public AliyunOSSOperator(AliyunOSSProperties aliyunOSSProperties) {
        this.aliyunOSSProperties = aliyunOSSProperties;
    } //利用构造器初始化成员变量

    public String upload(byte[] content, String originalFileName) throws ClientException {
        String endpoint = aliyunOSSProperties.getEndpoint();
        String bucketName = aliyunOSSProperties.getBucketName();
        String region = aliyunOSSProperties.getRegion();
        // 四：从环境变量中获取访问凭证AcessId和AcessSecrete，官网给，不用改动。运行本代码示例之前，请先配置环境变量
        EnvironmentVariableCredentialsProvider credentialsProvider =
                CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        //设置Object的完整路径，例如：2025/03/1.jpg
        //获取当前日期的字符串，格式yyyy/MM
        String dir = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM"));
        //重新生成一个不重复的名称
        String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
        String objectName = dir + "/" + newFileName;

        // 五：创建 OSSClient 实例，官网给，不用改动
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        // 显式声明使用 V4 签名算法
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .region(region)
                .build();
        try {
            // 上传文件到阿里云
            ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content));
        } finally {
            ossClient.shutdown();
        }
        /* https://keats-java-ai.oss-cn-beijing.aliyuncs.com/2025/03/1.jpg */
        return endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + objectName;
    }
}
